from fastapi import APIRouter
from pydantic import BaseModel

from service.task_site_service import TaskSiteService

router = APIRouter()


class TaskSiteCreate(BaseModel):
    type_name: str
    site_name: str
    list_link: str
    method: str
    param: str
    cookie: str
    user_agent: str
    status: int
    time_interval: int


@router.post("/task_sites/")
def create_task_site(task_site_data: TaskSiteCreate):
    service = TaskSiteService()
    return service.create_task_site(**task_site_data.dict())


@router.get("/task_sites/")
def read_task_sites(skip: int = 0, limit: int = 100):
    service = TaskSiteService()
    task_sites = service.get_all_task_sites()
    return task_sites[skip:skip + limit]


@router.get("/task_sites/{task_site_id}")
def read_task_site(task_site_id: int):
    service = TaskSiteService()
    task_site = service.get_task_site_by_id(task_site_id)
    if task_site is None:
        return {"error": "Task Site not found"}
    return task_site
